﻿<!DOCTYPE html>
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=0,minimum-scale=1.0,maximum-scale=1.0" />
    <meta name="author" content="火星科技 http://mars2d.cn " />
    <meta name="apple-touch-fullscreen" content="yes" />
    <meta name="apple-mobile-web-app-capable" content="yes" />
    <meta name="apple-mobile-web-app-status-bar-style" content="black" />
    <meta name="format-detection" content="telephone=no" />
    <meta name="x5-fullscreen" content="true" />
    <meta http-equiv="X-UA-Compatible" content="IE=Edge,chrome=1" />
    <link rel="shortcut icon" type="image/x-icon" href="https://cdn.jsdelivr.net/gh/muyao1987/cdn/marsgis.cn/favicon/favicon.ico" />

    <meta name="keywords" content="火星科技,leaflet,mars2d,GIS,marsgis,二维,地图,地球,开发,框架,系统,示例,资料,模型,离线,外包,合肥,安徽,中国" />
    <meta
      name="description"
      content="火星科技 合肥火星 合肥火星科技 合肥火星科技有限公司 leaflet leaflet框架 leaflet开发 leaflet leaflet开发 leaflet框架 二维 地图 模型  gis marsgis 地图离线 地图开发 地图框架 地图外包 框架 开发 外包  地图离线 二维地图 二维地图 全景漫游 地理信息系统 云GIS 二维GIS GIS平台 WebGIS"
    />

    <title>Mars2D地图 |二维地图| 火星科技|合肥火星科技|合肥火星科技有限公</title>

    <!--第三方lib-->
    <script
      type="text/javascript"
      src="../lib/include-lib.js"
      libpath="../lib/"
      include="jquery,font-awesome,bootstrap,layer,haoutil,turf,mars2d"
    ></script>

    <link href="css/style.css" rel="stylesheet" />
  </head>

  <body>
    <div id="mars2dContainer" class="mars2d-container"></div>

    <script src="./js/common.js"></script>
    <script type="text/javascript">
      "use script"; //开发环境建议开启严格模式

      var map;

      function initMap(options) {
        //合并属性参数，可覆盖config.json中的对应配置
        let mapOptions = mars2d.Util.merge(options, {
          zoom: 7,
          center: { lng: 116.378174, lat: 30.537774 },
        });

        //创建地图
        map = new mars2d.Map("mars2dContainer", mapOptions);

        //加载气象
        mars2d.Util.fetchJson({
          url: "http://data.mars2d.cn/file/apidemo/windpoint.json",
        })
          .then((res) => {
            showWindLine(res.data);
          })
          .catch((error) => {
            console.log("请求出错了", error);
            haoutil.msg("实时查询气象信息失败，请稍候再试");
          });
      }

      let colors = ["#006837", "#1a9850", "#66bd63", "#a6d96a", "#d9ef8b", "#ffffbf", "#fee08b", "#fdae61", "#f46d43", "#d73027", "#a50026"];
      let breaks = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 99]; //等值面的级数

      //等值线面
      function showWindLine(arr) {
        // var min = arr[0].speed
        // var max = arr[0].speed

        let pointGrid = [];
        for (let i = 0, len = arr.length; i < len; i++) {
          const item = arr[i];

          // if (min > item.speed) min = item.speed
          // if (max < item.speed) max = item.speed

          pointGrid.push({
            type: "Feature",
            properties: item,
            geometry: { type: "Point", coordinates: [item.x, item.y] },
          });
        }

        // breaks = []
        // var step = (max - min) / 10
        // for (var i = min; i <= max; i += step) {
        //   breaks.push(Number(i.toFixed(1)))
        // }

        let points = { type: "FeatureCollection", features: pointGrid };

        //插值
        // turf.interpolate提供了基于 IDW（反距离权重）算法的将数据插值为格点的方法。
        // 插值的精度是由第二个参数与 interpolate_options.units 共同决定的
        // IDW 要为每个格点计算所有散点的权重，计算规模是 (散点数 * 格点数)，所以要在精度与性能间做好平衡。
        // points = turf.interpolate(points, 10, {
        //   gridType: 'point', // 'square' | 'point' | 'hex' | 'triangle'
        //   property: 'speed',
        //   units: 'kilometers', // degrees, radians, miles, or kilometers
        //   weight: 1
        // })
        // 适当降低插值结果的精度便于显示
        // points.features.map((i) => (i.properties.speed = Number(i.properties.speed.toFixed(1))))

        //等值面
        let geojsonPoly = turf.isobands(points, breaks, { zProperty: "speed" });

        //创建矢量数据图层
        let geoJsonLayer = new mars2d.layer.GeoJsonLayer({
          name: "等值面",
          data: geojsonPoly,
          popup: "{speed}",
          symbol: {
            styleOptions: {
              fill: true, //是否填充
              fillColor: "#ffff00", //颜色
              fillOpacity: 0.7, //透明度
              outline: false,
            },
            callback: function (attr, styleOpt) {
              //得到点的权重，计算落在那个色度带
              let val = Number(attr.speed.split("-")[0] || 0);
              let color = getColor(val);
              return {
                fillColor: color,
              };
            },
          },
        });
        map.addLayer(geoJsonLayer);

        //等值线
        let geojsonLine = turf.isolines(points, breaks, { zProperty: "speed" });

        //进行平滑处理
        // var features = geojsonLine.features;
        // for (var i = 0; i < features.length; i++) {
        //     var _coords = features[i].geometry.coordinates;
        //     var _lCoords = [];
        //     for (var j = 0; j < _coords.length; j++) {
        //         var _coord = _coords[j];
        //         var line = turf.lineString(_coord);
        //         var curved = turf.bezierSpline(line);
        //         _lCoords.push(curved.geometry.coordinates);
        //     }
        //     features[i].geometry.coordinates = _lCoords;
        // }

        let layerDZX = new mars2d.layer.GeoJsonLayer({
          name: "等值线",
          data: geojsonLine,
          popup: "{speed}",
          symbol: {
            styleOptions: {
              width: 2, //边框宽度
              color: "#000000", //边框颜色
              opacity: 0.5, //边框透明度
            },
          },
        });
        map.addLayer(layerDZX);

        //与行政区划边界来裁剪整个色斑图。这里要用到 turf.intersect()
        //根据文档，这里输入的参数要 Feature<Polygon> ，而我们拿到的是 MultiPolygon，需要先 flatten() 处理一下。
        // geojsonPoly = turf.flatten(geojsonPoly);

        // var features = [];
        // geojsonPoly.features.forEach(function (layer1) {
        //     boundaries.features.forEach(function (layer2) {
        //         let intersection = null;
        //         try {
        //             intersection = turf.intersect(layer1, layer2);
        //         } catch (e) {
        //             layer1 = turf.buffer(layer1, 0); //容错处理
        //             intersection = turf.intersect(layer1, layer2);
        //         }
        //         if (intersection != null) {
        //             intersection.properties = layer1.properties;
        //             intersection.id = Math.random() * 100000;
        //             features.push(intersection);
        //         }
        //     });
        // });

        // var intersection = turf.featureCollection(features);
      }

      function getColor(value) {
        for (let i = 0; i < breaks.length; i++) {
          if (breaks[i] == value) {
            return colors[i];
          }
        }
        return colors[0];
      }
    </script>
  </body>
</html>
